package com.ygo.feihua.adapter;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import cn.bmob.v3.datatype.BmobFile;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ygo.feihua.BmobTable.MyUser;
import java.util.List;
import java.util.Map;
import com.ygo.feihua.*;
import com.ygo.feihua.BmobTable.*;
import android.support.v7.widget.*;
import java.util.*;
import java.text.*;
import android.widget.*;

public class ShequAdapter extends RecyclerView.Adapter<ShequAdapter.ViewHolder>
{

	@Override
	public ShequAdapter.ViewHolder onCreateViewHolder(ViewGroup p1, int p2)
	{
		View v=LayoutInflater.from(p1.getContext()).inflate(R.layout.item_shequ,p1,false);
		
		// TODO: Implement this method
		return new ViewHolder(v);
	}

	@Override
	public void onBindViewHolder(ShequAdapter.ViewHolder vh, final int position)
	{
		vh.sq_title.setText(data.get(position).gettitle());

		ParsePosition pp=new ParsePosition(0);
		Date date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((data.get(position).getUpdatedAt()),pp);
		String time=(1900+date.getYear())+"-"+(date.getMonth()+1)+"-"+date.getDate();
		vh.sq_time.setText(time);
		Integer viewed=data.get(position).getViewed();
		if(viewed!=null){
		vh.sq_viewed.setText(""+viewed);
		}else{
		vh.sq_viewed.setText(""+0);
		}
		vh.sq_pls.setText(""+data.get(position).getPls());
		vh.sq_cv.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					String id=data.get(position).getObjectId();
					if(id==null){
						gj.show("没有该帖子的信息");
					}else{
						gj.tieziIntent(id);
					}
					// TODO: Implement this method
				}
			});
		
		gj.setTouxiang(vh.sq_touxiang,data.get(position).gettiezuo());
		if(data.get(position).getneirong().equals("")){
			String ss="";
			int aa=0;
			for(TieziMessage tm:data.get(position).getMessage()){

				if(tm.getImage()!=null){
					if(aa==0){
						vh.sq_tpl.setVisibility(View.VISIBLE);
						gj.tuxian(tm.getImage(),vh.sq_tp);
					}
					aa++;
					ss+="[图片]\n";
				}
				ss+=tm.getMessage()+"\n";
			}
			ss.trim();
			if(aa>1){
				vh.sq_tps.setText(aa+"");
				vh.sq_tps.setVisibility(View.VISIBLE);
			}else{
				vh.sq_tps.setVisibility(View.GONE);
			}
			if(aa==0){
				vh.sq_tpl.setVisibility(View.GONE);
			}
			vh.sq_neirong.setText(ss);
		}else{
			vh.sq_neirong.setText(data.get(position).getneirong());
			if(data.get(position).getImage()==null){
				vh.sq_tpl.setVisibility(View.GONE);
			}else{
				vh.sq_tpl.setVisibility(View.VISIBLE);
				List<BmobFile> bf=data.get(position).getImage();
				gj.tuxian(bf.get(0).getFileUrl(),vh.sq_tp);
				if(bf.size()>1){
					vh.sq_tps.setText(bf.size()+"");
					vh.sq_tps.setVisibility(View.VISIBLE);
				}else{
					vh.sq_tps.setVisibility(View.GONE);
				}
			}
		}
		vh.sq_touxiang.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					Intent intent=new Intent(context,YonghuMingpian.class);
					String id=data.get(position).gettiezuo().getObjectId();
					if(id==null){
						gj.show("没有该用户的信息");
					}else{
						intent.putExtra("id",id);
						context.startActivity(intent);
					}
					// TODO: Implement this method
				}
			});
		
			
		// TODO: Implement this method
	}

	@Override
	public int getItemCount()
	{
		// TODO: Implement this method
		return data.size();
	}
	
	List<Tiezi> data;
	Context context;
    Gongju gj;

	public ShequAdapter(Context context, List<Tiezi> data)
	{
		this.context = context;
		this.data=data;
        gj=Gongju.getdx(context);
	}
    public void sx(){
        notifyDataSetChanged();
    }

	class ViewHolder extends RecyclerView.ViewHolder{
		public ImageView image;  
		public TextView sq_title,sq_neirong,sq_time,sq_pls,sq_viewed;  
		public Button sq_zan;  
		public TextView info,sq_tps;  
		public ImageView sq_touxiang,sq_tp;
		RelativeLayout sq_tpl;
		CardView sq_cv;
		
		View v;
		public ViewHolder(View v){
			super(v);
			this.v=v;
			sq_title=(TextView) v.findViewById(R.id.sq_title);
			sq_neirong=(TextView) v.findViewById(R.id.sq_neirong);
			sq_touxiang=(ImageView) v.findViewById(R.id.sq_touxiang);
			sq_time=(TextView) v.findViewById(R.id.sq_time);
			sq_pls=(TextView) v.findViewById(R.id.sq_pls);
			sq_tps=(TextView) v.findViewById(R.id.sq_tps);
			sq_tpl=(RelativeLayout) v.findViewById(R.id.sq_tpl);
			sq_cv=(CardView) v.findViewById(R.id.sq_cv);
			sq_tp=(ImageView) v.findViewById(R.id.sq_tp);
			sq_viewed=(TextView) v.findViewById(R.id.sq_viewed);
		}
	}

}
